<?php
/**
 * 分页类
 * 
 *  @author by wq
 *  @date 2013-12-09
 *
 */
class LibsPage {
    private static $_noA = '...';
    public static $pageArr = array();

    public static function getPage($currentPage, $totalPage, $url) {
        self::$pageArr = self::setPage($currentPage, $totalPage);
        $str = '<div id="page" class="pages">';
        foreach (self::$pageArr as $k => $v) {
            if (!$k) {
                if ($currentPage == 1) {
                    $str .= "<em class='prev'>{$v}</em>";
                } else {
                    $str .= LibsCommon::action($url.'/'.($currentPage-1), $v, array('class'=>'prev'));
                }
            } elseif ($currentPage == $k) {
                $str .= "<span>{$v}</span>";
            } elseif ($k > $totalPage) {
                if ($currentPage == $totalPage) {
                    $str .= "<em class='next'>{$v}</em>";
                } else {
                    $str .= LibsCommon::action($url.'/'.($currentPage+1), $v, array('class'=>'next'));
                }
            } elseif($v == self::$_noA) {
                $str .= "<a>{$v}</a>";
            } else {
                $str .= LibsCommon::action($url.'/'.$k, $v);
            }
        }
        $str .= "</div>";
        return $str;
    }

    /**
     * 分页数组
     *
     *
     */
    public static function setPage($page, $totalPage) {
        self::$pageArr = array(0=>'上一页');

        if ($page>5) {
            self::$pageArr[1] = 1;
            self::$pageArr[] = self::$_noA;
            for($i=$page-2; $i<=$page; $i++) {
                self::$pageArr[$i] = $i;
            }
        } else {
            for ($i=1; $i<=$page; $i++) {
                self::$pageArr[$i] = $i;
            }
        }

        if ($totalPage>$page+5) {
            for($i=$page+1; $i<=$page+2; $i++) {
                self::$pageArr[$i] = $i;
            }
            self::$pageArr[] = self::$_noA;
            for($i=$totalPage-2; $i<=$totalPage; $i++) {
                self::$pageArr[$i] = $i;
            }
        } else {
            for($i=$page; $i<=$totalPage; $i++) {
                self::$pageArr[$i] = $i;
            }
        }
        self::$pageArr[] = '下一页';
        return self::$pageArr;
    }


}

